package cn.action;

import cn.base.BaseAction;
import cn.domain.*;
import cn.utils.HQLHelper;
import org.apache.struts2.ServletActionContext;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/7 0007.
 */
public class ReplyAction extends BaseAction<Reply> {

    /**
     * 添加回复
     */
    public String add(){
        System.out.println(model.getContent());
        //获取ip
        String ip= ServletActionContext.getRequest().getRemoteAddr();
        model.setIpAddress(ip);

        //获取时间
        Date date=new Date();
        model.setPostTime(date);

        //获取用户
        User user= (User) ServletActionContext.getRequest().getSession().getAttribute("user");
        model.setAuthor(user);

        //获取再那个板块发的主题
        Long id = model.getId();
        Topic topic=topicService.getById(id);

        //设置保存板块
        model.setTopic(topic);

        //保存数据
        replyService.save(model);
        Long tid= (Long) ServletActionContext.getRequest().getSession().getAttribute("id");
        Topic topicq=topicService.getById(tid);
        getValueStack().set("topic",topicq);
        HQLHelper hqlHelper=new HQLHelper(Reply.class);
        hqlHelper.addWhere("topic=?",model);
        hqlHelper.orDerBy("postTime",true);
        PageBean pb=replyService.getPageBean(hqlHelper,getCurrentPage());
        getValueStack().push(pb);
        model.setId(tid);
        return "list";
    }

    /**
     * 删除回复
     * @return
     */
    public String delete(){
       Long id    =  model.getId();
       replyService.delete(id);
        return "list";
    }

}
